=head
open BUFF,"../genotyping_imp_workflow_QC_ambi_xchr_2valDos_V2/bin/files/newhapmap3_sample_run.tped" or die "no hapmap files exists\n";
$n = 0;
while(<BUFF>)
{
	if($_ =~ m/^23/)
	{
		@array = split(" ",$_);
		$l = "";
		if($_ =~ m/A/)
		{
			$l = $l."A";
		}
		if($_ =~ m/C/)
                {
                        $l = $l."C";
                }
		if($_ =~ m/G/)
                {
                        $l = $l."G";
                }
		if($_ =~ m/T/)
                {
                        $l = $l."T";
                }

		$hash{$array[1]} = $l;
		$hash2{$array[1]} = $_;
		$pos{$array[1]} = $array[3]; 
		$n++;
	}
}
print "$n\n";
open BUFF,"./untouch_xchrimputationfiles/markers.txt" or die "no markers.txt file exists\n";
open BUFF1,"./untouch_xchrimputationfiles/phasedfemale.bgl" or die "no phasedfemale.bgl files exists\n";
open BUFF2,"./untouch_xchrimputationfiles/phasedmale.bgl" or die "no phasedmale.bgl files exists\n";
open(WRBUFF,">markers_new.txt");
open(WRBUFF1,">phasedfemale_new.bgl");
open(WRBUFF2,">phasedmale_new.bgl");
$femaley = <BUFF1>;
print WRBUFF1 $femaley;
$maley = <BUFF2>;
print WRBUFF2 $maley;
while(<BUFF>)
{
	chomp($_);
	$male = <BUFF2>;
	$female = <BUFF1>;
	$male_female = $male.$female;
	@male = split(" ",$male);
	@female = split(" ",$female);
	@array = split(" ",$_);
	$hash1{$array[0]} =1;
	if(exists($hash{$array[0]}))
	{
		$array[1] = $pos{$array[0]};
		 $l = "";
                if($male_female =~ m/A/)
                {
                        $l = $l."A";
                }
                if($male_female =~ m/C/)
                {
                        $l = $l."C";
                }
                if($male_female =~ m/G/)
                {
                        $l = $l."G";
                }
                if($male_female =~ m/T/)
                {
                        $l = $l."T";
                }
		@l = split('',$l);
		@l = sort(@l);
		$l = join("",@l);
		@l = split('',$hash{$array[0]});
		@l = sort(@l);
		$hash{$array[0]} = join("",@l);
		$m = $l;
		$m =~ tr/ACGT/TGCA/;
		@l = split('',$m);
		@l = sort(@l);
		$m = join("",@l);
		$_ = join(" ",@array);
		if($l eq $hash{$array[0]})
		{
			print WRBUFF $_."\n";
			print WRBUFF2 $male;
			print WRBUFF1 $female;
		}
		elsif($m eq $hash{$array[0]})
		{	
			$_ =~ tr/ACGT/TGCA/;
			$male =~ tr/ACGT/TGCA/;
			$female =~ tr/ACGT/TGCA/;
			print WRBUFF $_."\n";
                        print WRBUFF2 $male;
                        print WRBUFF1 $female;
		}
		else
		{
			die "$array[0]\t$l\t$m\t$hash{$array[0]}\n";
		}
	}
}
@array = split(" ",$femaley);
shift(@array);
shift(@array);
#print "female $female male $male\n";
for($i=0;$i<@array;$i++)
{
	$gender{$array[$i]} = "female";
	$female{$array[$i]} = $i;
}
@array = split(" ",$maley);
shift(@array);
shift(@array);
for($i=0;$i<@array;$i++)
{
        $gender{$array[$i]} = "male";
	$male{$array[$i]} = $i;
}
open(BUFF,"../genotyping_imp_workflow_QC_ambi_xchr_2valDos_V2/bin/files/newhapmap3_sample_run.tfam");
$i=0;
while(<BUFF>)
{
	chomp($_);
	@array = split(" ",$_);
	#if(exists($gender{$array[1]}))
	#{
		if($gender{$array[1]} eq "female")
		{
			$nu1 = $female{$array[1]}*2;
			$nu2 = $nu1+1;	
			push(@order,"f$nu1");
			push(@order,"f$nu2");
			$i++;
			#$i++;
		}
		elsif($gender{$array[1]} eq "male")
                {
			$nu1 = $male{$array[1]}*2;
                        $nu2 = $nu1+1;
                        push(@order,"m$nu1");
                        push(@order,"m$nu2");
			$i++;
			#$i++;
                }
		else
		{
			push(@order,"0");
			push(@order,"0");
		}

	#}
}
while(($key,$value)=each %hash2)
{
	if(!exists($hash1{$key}))
	{
		print "$i sucess $key @order\n";
		print @order."\n";
	}
}
close(WRBUFF);
close(WRBUFF1);
close(WRBUFF2);
=cut
#sorting the xchr files
open(BUFF,"markers_new.txt") or die "no file mark found\n";
open(BUFF1,"phasedfemale_new.bgl") or die " no file phasedfemale found\n";
open(BUFF2,"phasedmale_new.bgl") or die " no phasedmale found\n";

open(WRBUFF1,">phasedfemale.bgl");
open(WRBUFF2,">phasedmale.bgl");
$femaley = <BUFF1>;
print WRBUFF1 $femaley;
$maley = <BUFF2>;
print WRBUFF2 $maley;
system("sort -k2,2n markers_new.txt > markers.txt");
open(BUFF,"markers.txt");
$k = 0;
while(<BUFF>)
{
	chomp($_);
	@array = split(" ",$_);
	$hash{$array[0]} = $k;
	$k++;
}
#sorting female markers
@female_a = ();
while(<BUFF1>)
{
	chomp($_);
	@array = split(" ",$_);
	$female_a[$hash{$array[1]}] = $_;
}
for($i=0;$i<@female_a;$i++)
{
	print WRBUFF1 $female_a[$i]."\n";
}

#sorting male markers
@male_a = ();
while(<BUFF2>)
{
	chomp($_);
	@array = split(" ",$_);
	$male_a[$hash{$array[1]}] = $_;
}
for($i=0;$i<@male_a;$i++)
{
	print WRBUFF2 $male_a[$i]."\n";
}


